package org.dromara.system.controller.autocode;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.autocode.domain.entity.SysAutoCodeRule;
import org.dromara.autocode.service.SysAutoCodeRuleService;
import org.dromara.common.core.domain.PageData;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.web.core.BaseService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 编码生成规则
 *
 * @author wenrui Su
 * @date 2025-03-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/autoCodeRule")
public class SysAutoCodeRuleController extends BaseService {

  private final SysAutoCodeRuleService sysAutoCodeRuleService;

/**
 * 查询编码生成规则列表
 */
@SaCheckPermission("sys:autoCodeRule:list")
@GetMapping("/list")
  public PageData list(SysAutoCodeRule bo, PageQuery pageQuery) {
    return this.resPage(sysAutoCodeRuleService.queryPageList(bo, pageQuery));
  }

  /**
   * 导出编码生成规则列表
   */
  @SaCheckPermission("sys:autoCodeRule:export")
  @Log(title = "编码生成规则", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(SysAutoCodeRule bo, HttpServletResponse response) {
    List<SysAutoCodeRule> list = sysAutoCodeRuleService.queryList(bo);
    ExcelUtil.exportExcel(list, "编码生成规则", SysAutoCodeRule. class,response);
  }

  /**
   * 获取编码生成规则详细信息
   *
   * @param ruleId 主键
   */
  @SaCheckPermission("sys:autoCodeRule:query")
  @GetMapping("/{ruleId}")
  public PageData getInfo(@NotNull(message = "主键不能为空") @PathVariable Long ruleId) {
    return this.resData(sysAutoCodeRuleService.queryById(ruleId));
  }

  /**
   * 新增编码生成规则
   */
  @SaCheckPermission("sys:autoCodeRule:add")
  @Log(title = "编码生成规则", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping()
  public PageData add(@Validated(AddGroup.class) @RequestBody SysAutoCodeRule bo) {
    return this.resData(sysAutoCodeRuleService.insertByBo(bo));
  }

  /**
   * 修改编码生成规则
   */
  @SaCheckPermission("sys:autoCodeRule:edit")
  @Log(title = "编码生成规则", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping()
  public PageData edit(@Validated(EditGroup.class) @RequestBody SysAutoCodeRule bo) {
    return this.resData(sysAutoCodeRuleService.updateByBo(bo));
  }

  /**
   * 删除编码生成规则
   *
   * @param ruleIds 主键串
   */
  @SaCheckPermission("sys:autoCodeRule:remove")
  @Log(title = "编码生成规则", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ruleIds}")
  public PageData remove(@NotEmpty(message = "主键不能为空")
                        @PathVariable Long[] ruleIds) {
    return this.resData(sysAutoCodeRuleService.deleteWithValidByIds(List.of(ruleIds), true));
  }
}
